package com.yida.gwj.vote.outside.controller;

import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.vote.outside.domain.GwjVoteProject;
import com.yida.gwj.vote.outside.domain.dto.GwjBaseBuildHouseOwnerDTO;
import com.yida.gwj.vote.outside.domain.request.GwjVoteBuildSelectRequest;
import com.yida.gwj.vote.outside.service.IGwjBaseDataService;
import com.yida.gwj.vote.outside.service.IGwjVoteProjectService;
import com.yida.gwj.common.valid.CancelSelect;
import com.yida.gwj.common.valid.ConfirmSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 投票楼栋Controller
 *
 * @author lqq
 * @date 2023-03-22
 */
@RestController
@RequestMapping("/build")
@Api(tags = "投票楼栋")
public class GwjVoteBuildController extends BaseController {

	@Resource
	private IGwjVoteProjectService voteProjectService;

	@Resource
	private IGwjBaseDataService baseBuildService;

	@ApiOperation("加入选择")
	@RequiresPermissions("gwj:vote:outside:e:project:add")
	@PostMapping("/confirmSelect")
	@Log(title = "公维金外网投票-楼栋加入选择", businessType = BusinessType.INSERT, systemName = "vote")
	public AjaxResult confirmSelect(@Validated(ConfirmSelect.class) @RequestBody GwjVoteBuildSelectRequest request) {
		//修改时投票信息是验证状态
		GwjVoteProject project = voteProjectService.checkProjectForUpdate(request.getVoteId());
		//查询基础数据楼栋、房屋、业主所有数据
		GwjBaseBuildHouseOwnerDTO dto = baseBuildService.getAllDataByBuildIds(request.getBuildIds(), request.getAreaId());
		dto.setCommunityId(project.getCommunityId());
		dto.setVoteId(request.getVoteId());
		return toAjax(voteProjectService.confirmSelectBuild(dto));
	}

	@ApiOperation("取消选择或删除楼栋")
	@RequiresPermissions("gwj:vote:outside:e:project:add")
	@PostMapping("/cancelSelect")
	@Log(title = "公维金外网投票-楼栋取消选择/删除楼栋", businessType = BusinessType.DELETE, systemName = "vote")
	public AjaxResult cancelSelect(@Validated(CancelSelect.class) @RequestBody GwjVoteBuildSelectRequest request) {
		//修改时投票信息是验证状态
		voteProjectService.checkProjectForUpdate(request.getVoteId());
		return toAjax(voteProjectService.cancelSelectBuild(request.getBuildIds(), request.getVoteId()));
	}
}
